home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Online / SpeakFreely / src / adpcm / suncaudio.c < prev    next >
C/C++ Source or Header  |  2000-05-18  |  4KB  |  153 lines

  1. #include <stdio.h>
  2. #include <strings.h>
  3. #include <fcntl.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <multimedia/ulaw2linear.h>
  7. #include <multimedia/libaudio.h>
  8. #include "adpcm.h"
  9.  
  10. #ifdef __STDC__
  11. #define A(x) x
  12. #else
  13. #define A(x) ()
  14. #endif
  15.  
  16. extern char *strdup A((char *));
  17. extern char *strstr A((char *, char *));
  18.  
  19.  
  20. main(argc, argv)
  21.     int argc;
  22.     char *argv[];
  23. {
  24.  
  25. int file,outfile;        /* input file */
  26. Audio_hdr audiohdr;        /* Audio File header */
  27. int cnt;            /* how much read ? */
  28. register char *ulaw;        /* pointer to ulaw output from pcm conv */
  29. register short *pcm;        /* pointer to pcm output from ulaw conv */
  30. char ulawbuf[BUFSIZ];        /* to hold ulaw data */
  31. short pcmbuf[BUFSIZ];        /* to hold pcm data */
  32. int decompress=0;        /* just a flag */
  33. struct stat statbuf;
  34. char *newfn;            /* output file name */
  35. char *p;            /* temporary character pointer */
  36. struct adpcm_state state;
  37. int pos;
  38.  
  39.  
  40. /* this program can be called with two different names, if it
  41.    is called as caudio then we want to compress the file and if
  42.    it is called with uaudio then we want to uncompress it */
  43.  
  44.         pos = strlen(argv[0]) - strlen("caudio");
  45.         if (strcmp(argv[0]+pos,"daudio") == 0) 
  46.         decompress=1;
  47.         else if ( strcmp(argv[0]+pos,"caudio") != 0) {
  48.         puts("Progname should end in 'caudio' or 'daudio'");
  49.         exit(1);
  50.     }
  51.  
  52.     if (argc < 2) {
  53.         puts("No input file(s) specified");
  54.         exit(1);
  55.     }
  56.  
  57. /* Now we pass through the entire list of input files and compress
  58.    each file found */
  59.  
  60.     while (argc-- > 1) {
  61.         if ((file = open(*++argv,O_RDONLY)) == NULL) {
  62.             perror("couldn't open input file");
  63.             exit(1);
  64.         }
  65.         if (!decompress) {
  66.             if (! audio_isaudiofile(*argv)) {
  67.                 perror("not an audio file");
  68.                 exit(2);
  69.             }
  70.             if (audio_read_filehdr(file, &audiohdr, 
  71.                     (char *)NULL, 0) != AUDIO_SUCCESS) {
  72.                 perror("bad header");
  73.                 exit(3);
  74.             }
  75.             newfn=(char *) malloc(strlen(*argv)+5);
  76.             sprintf(newfn,"%s.pcm",*argv);
  77.  
  78.             if ((outfile = open(newfn, O_RDWR |O_CREAT | O_EXCL,
  79.                         0644)) < 0) {
  80.                 printf("%s already exists\n",newfn);
  81.                 free(newfn);
  82.                 continue;
  83.             }
  84.             while ((cnt = read(file, (char *) ulawbuf,
  85.                        sizeof ulawbuf)) > 0) {
  86.                 ulaw = ulawbuf;
  87.                 pcm = pcmbuf;
  88.  
  89.                 /* translate all from ulaw to 16 bit pcm */
  90.                 while (ulaw != ulawbuf+cnt) {
  91.                     *pcm++ = (ushort) audio_u2s(*ulaw++);
  92.                 }
  93.  
  94.                 /* now quantize it using adpcm */
  95.                 adpcm_coder(pcmbuf, ulawbuf, cnt, &state);
  96.                 write(outfile, ulawbuf, cnt/2);
  97.             }
  98.             close(outfile);
  99.             free(newfn);
  100.         }
  101.         else {
  102.             newfn=strdup(*argv);
  103.             if ((p=strstr(newfn,".pcm")) == NULL) {
  104.                 puts("not a valid pcm file");
  105.                 continue;
  106.             }
  107.             *p='\0';        /* take off .pcm suffix */
  108.  
  109.             if ((outfile = open(newfn, O_RDWR |O_CREAT | O_EXCL,
  110.                         0644)) < 0) {
  111.                 printf("%s already exists\n",newfn);
  112.                 free(newfn);
  113.                 continue;
  114.             }
  115.             fstat(file,&statbuf);
  116.             audiohdr.sample_rate = 8000;
  117.             audiohdr.samples_per_unit = 1;
  118.             audiohdr.bytes_per_unit = 1;
  119.             audiohdr.channels = 1;
  120.             audiohdr.encoding = AUDIO_ENCODING_ULAW;
  121.  
  122.             /* now tell it how big the audio file is. The original
  123.              * file has exactly twice as many playable bytes as
  124.              * this one, so we double the current number.
  125.              * There are also 32Bytes in
  126.              * the sound header, but they don't count */
  127.             audiohdr.data_size = statbuf.st_size * 2;
  128.  
  129.             if (audio_write_filehdr(outfile, &audiohdr, NULL, 0) 
  130.                             != AUDIO_SUCCESS) {
  131.                 perror("writing header");
  132.                 exit(5);
  133.             }
  134.             while ((cnt = read(file, ulawbuf, sizeof(ulawbuf)/2))
  135.                                                  > 0) {
  136.                 ulaw = ulawbuf;
  137.                 pcm = pcmbuf;
  138.  
  139.                 /* first we decode from adpcm format */
  140.                 adpcm_decoder(ulawbuf, pcmbuf, cnt*2, &state);
  141.  
  142.                 /* now translate from 16bit pcm to Sun
  143.                  * mulaw format */
  144.                 while (pcm != pcmbuf+cnt*2) {
  145.                     *ulaw++ = audio_s2u(*pcm++);
  146.                 }
  147.                 write(outfile, ulawbuf, cnt*2);
  148.             }
  149.         }
  150.     }
  151.     exit(0);
  152. }
  153.